package com.faya.green.service.impl;

import com.faya.green.abnormal.ServiceException;
import com.faya.green.common.Constant;
import com.faya.green.dao.user.CommendDao;
import com.faya.green.dao.user.UserDao;
import com.faya.green.enumeration.StatusEnum;
import com.faya.green.model.Commend;
import com.faya.green.model.User;
import com.faya.green.service.CommendService;
import com.faya.green.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ：LX
 * 创建时间： 2018/8/24. 17:54
 * 地点：广州
 * 目的: 嘉奖信息
 * 备注说明：
 */
@Service
public class CommendServiceImpl implements CommendService {

    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private CommendDao commendDao;
    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private UserDao userDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCommend(Commend commend) {
        if (CommonUtils.checkNull(commend.getUserId())){
            throw new ServiceException("用户ID找不到");
        }
        commend.setStatus(StatusEnum.STATUS_UPDATE.getCode());
        commend.setUpdateTime(new Date());

        commendDao.updateCommend(commend);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addCommend(Commend commend) {
        if (CommonUtils.checkNull(commend.getUserId())){
            throw new ServiceException("用户ID找不到");
        }
        User user = userDao.findUserById(commend.getUserId());
        if (user == null){
            throw new ServiceException("找不到用户");
        }
        user.setIsCommend(Constant.YES);
        userDao.updateUser(user);

        commend.setStatus(StatusEnum.STATUS_ADD.getCode());
        commend.setId(UUID.randomUUID().toString());
        commend.setCreateTime(new Date());
        commend.setUpdateTime(new Date());
        commendDao.addCommend(commend);


    }

    @Override
    public Commend getCommendById(String id) {
        return commendDao.getCommendById(id);
    }

    @Override
    public List<Commend> getCommendByUserId(String userId) {
        return commendDao.getCommendByUserId(userId);
    }

    @Override
    public List<Commend> getCommendByUserIdPulic(String userId, String isPubilc) {
        return commendDao.getCommendByUserIdPulic(userId, isPubilc);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delCommend(String id) {
        Commend commend = commendDao.getCommendById(id);
        if (commend != null){
            String userId = commend.getUserId();
            List<Commend> commendList = commendDao.getCommendByUserId(userId);
            if (commendList.size() == 1 || commendList.size() == 0){
                //说明只有这一条数据了
                User user = userDao.findUserById(userId);
                user.setIsCommend(Constant.NO);
                userDao.updateUser(user);
            }
            commendDao.delCommend(id);
        }
    }
}
